package com.st.bottomnavigation.ui.adapter.test;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.st.bottomnavigation.R;
import com.st.bottomnavigation.bean.test.Student;
import com.st.bottomnavigation.service.TestService;
import com.st.bottomnavigation.ui.activity.BaseActivity;
import com.st.bottomnavigation.ui.activity.GroupActivity;
import com.st.bottomnavigation.utils.Data;
import com.st.bottomnavigation.utils.RetrofitUtil;

import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 停点名称列表-适配类
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    BaseActivity baseActivity;
    ArrayList<Student> list;//车位数据
    private OnItemClickListener onItemClickListener;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_student, parent, false);
        return new ViewHolder(view);
    }

    // 定义接口用于回调点击事件
    public interface OnItemClickListener {
        void onItemClick(View v,int position);
    }

    public RecyclerViewAdapter(ArrayList<Student> list, BaseActivity baseActivity) {
        this.list = list;
        this.baseActivity = baseActivity;
    }

    // 设置点击事件监听器
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    public void setData(ArrayList<Student> studentList) {
        list = studentList;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, int position) {
        ViewHolder holder = (ViewHolder) viewHolder;
        Student student = list.get(position);
        holder.tv1.setText(student.getStuNo());
        holder.tv3.setText(student.getStuName());
        holder.tv2.setText(student.getSex());
        holder.detailBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (holder.detail.getVisibility() == View.GONE){//打开详情

                    TestService testService = RetrofitUtil.build(TestService.class);//初始化网络请求
                    holder.second_list.setLayoutManager(new LinearLayoutManager(holder.second_list.getContext()));
                    //每次请求前清空
                    BaseActivity activity = baseActivity;
                    //请求后端数据
                    testService.getList().enqueue(new Callback<ResponseBody>() {
                        @Override
                        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                            try {
                                JSONObject data = RetrofitUtil.getDataFromResponse(response);
                                JSONArray rows = Data.getJSONArray(data, "data", new JSONArray());
                                ArrayList<Student> list = new ArrayList<>();
                                for (int i = 0; i < rows.size(); i++) {
                                    Student student = new Student();
                                    JSONObject json = rows.getJSONObject(i);
                                    student.setStuNo(Data.getString(json,"userNo",""));
                                    student.setStuName(Data.getString(json,"userName",""));
                                    student.setSex(Data.getString(json,"sex",""));
                                    list.add(student);
                                }

                                activity.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        DetailAdapter adapter = new DetailAdapter(list, activity);
                                        holder.second_list.setAdapter(adapter);
                                    }
                                });
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onFailure(Call<ResponseBody> call, Throwable e) {
                            System.out.println(e.getMessage());
                        }
                    });

                    // 展示详情
                    holder.detailBtn.setBackgroundResource(R.drawable.icon_detail_selected);  // 设置背景资源
                    holder.detailText.setTextColor(Color.parseColor("#3b82c2"));   // 设置字体颜色
                    holder.detail.setVisibility(View.VISIBLE);
                }else {

                    //关闭详情
                    holder.detailBtn.setBackgroundResource(R.drawable.linearlayout_border);  // 设置背景资源
                    holder.detailText.setTextColor(Color.parseColor("#515254"));   // 设置字体颜色
                    holder.detail.setVisibility(View.GONE);
                }
            }
        });

    }

    public final class ViewHolder extends RecyclerView.ViewHolder {
        TextView tv1;
        TextView tv2;
        TextView tv3;
        TextView detailText;
        LinearLayout detailBtn;
        LinearLayout detail;
        RecyclerView second_list;

        public ViewHolder(View itemView) {
            super(itemView);
            tv1 = itemView.findViewById(R.id.tv1);
            tv2 = itemView.findViewById(R.id.tv2);
            tv3 = itemView.findViewById(R.id.tv3);
            detailText = itemView.findViewById(R.id.detailText);
            detailBtn = itemView.findViewById(R.id.detailBtn);
            detail = itemView.findViewById(R.id.detail);
            second_list = itemView.findViewById(R.id.second_list);
        }
    }

    @Override
    public long getItemId(int index) {
        return index;
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }


}
